package com.bdcom.comp;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;

import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class SignSlider extends JSlider{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private CanvasPanel canvasPanel;
	
	private volatile boolean enableListener;

	public SignSlider() {
		super();
		// TODO Auto-generated constructor stub
		
		setInverted(true);

		setMajorTickSpacing(10);

		setPaintTicks(true);

		setPaintLabels(true);

		setUI(new mySliderUI());
		
		addChangeListener( new ChangeListener(){
			
            public void stateChanged(ChangeEvent e){
            	
            	if(canvasPanel.getCanvasModel()==null){
            		return;
            	}
            	
            	if(enableListener){
            		
            		if (e.getSource() instanceof JSlider) {
            			//设置最小显示的信号.
            			canvasPanel.getCanvasModel().setMinSignToShow(((JSlider) e.getSource()).getValue());
            		}
            		
            	}
            }
        });
		
	}
	
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		
		enableListener=false;
		
		if(canvasPanel.getCanvasModel()==null){
			setMinimum(-85);
			setMaximum(-10);
			setValue(-70);
		}else
		{
			setMinimum(canvasPanel.getCanvasModel().getMinSign());
			setMaximum(canvasPanel.getCanvasModel().getMaxSign());
			setValue(canvasPanel.getCanvasModel().getMinSignToShow());
		}
		enableListener=true;
		
	}

}

class mySliderUI extends javax.swing.plaf.metal.MetalSliderUI {
	
	@Override
	public void paintThumb(Graphics g) {
		// TODO Auto-generated method stub
		Graphics2D g2d = (Graphics2D) g;
		
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		// 填充椭圆框为当前thumb位置
		g2d.fillOval(thumbRect.x, thumbRect.y, thumbRect.width,
				thumbRect.height);
	}
	
	@Override
	public void paintTrack(Graphics g) {
		// TODO Auto-generated method stub
		int cy, cw;
		
		Rectangle trackBounds = trackRect;
		
		if (slider.getOrientation() == JSlider.HORIZONTAL) {
			
			Graphics2D g2 = (Graphics2D) g;
			
			cy = trackBounds.height / 2;
			
			cw = trackBounds.width;
			
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			g2.translate(trackBounds.x, trackBounds.y + cy);
			
			g2.setPaint(Color.GRAY);
			
			g2.fillRect(0, -cy, cw, cy * 2);
			
			int trackLeft = 0;
			
			int middleOfThumb = 0;
			
			int fillLeft = 0;
			
			int fillRight = 0;
			
			middleOfThumb = thumbRect.x + (thumbRect.width / 2);
			
			middleOfThumb -= trackRect.x;
			
			fillLeft = !slider.isEnabled() ? trackLeft : trackLeft + 1;
			
			fillRight = middleOfThumb;
			
			g2.setPaint(new GradientPaint(0, 0, new Color(0, 0, 200), cw/2,
					0, new Color(0,200,0), true));
		
			if((fillRight-fillLeft)<=cw/2){
				
				g2.fillRect(0, -cy, fillRight - fillLeft, cy * 2);
				
			}else{
				
				g2.fillRect(0, -cy, cw/2, cy * 2);
				
				g2.setPaint(new GradientPaint(cw/2, 0, new Color(0,200, 0), cw,
			
						0, new Color(200,0,0), true));
				
				g2.fillRect(cw/2, -cy,(fillRight - fillLeft)-cw/2, cy * 2);
				
			}
			
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_OFF);
			g2.translate(-trackBounds.x, -(trackBounds.y + cy));
		} else {
			super.paintTrack(g);
		}
	}
	
}
